package cn.peach.hrm.service.impl;

import cn.peach.hrm.constants.BaseConstants;
import cn.peach.hrm.constants.RedisConstants;
import cn.peach.hrm.domain.Login;
import cn.peach.hrm.domain.VipBase;
import cn.peach.hrm.domain.VipUser;
import cn.peach.hrm.dto.UserRegisterDto;
import cn.peach.hrm.feign.client.LoginFeignClient;
import cn.peach.hrm.mapper.VipBaseMapper;
import cn.peach.hrm.mapper.VipUserMapper;
import cn.peach.hrm.service.IVipUserService;
import cn.peach.hrm.to.SendVerifyCodeTo;
import cn.peach.hrm.util.AjaxResult;
import cn.peach.hrm.util.AssertUtil;
import cn.peach.hrm.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author peach.ja
 * @since 2021-12-02
 */
@Service
@Transactional
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private LoginFeignClient loginFeignClient;

    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Override
    public void register(UserRegisterDto dto) {
        /** 1.判断参数
        * 2.判断手机是否已经被注册
        * 3.判断手机验证码
        * 3.1.获取Redis中的手机验证码
        * 3.2.和传入的手机验证码比较
        * 4.保存Login
        * 5.保存User
        * 6.保存Base
        * 7.删除手机验证码*/
        // 获取参数
        String mobile = dto.getMobile();
        String password = dto.getPassword();
        String smsCode = dto.getSmsCode();
        Integer regChannel = dto.getRegChannel();
        // 手机号判断
        AssertUtil.isNotEmpty(mobile, "手机号无效");
        EntityWrapper<VipUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", mobile);
        // 拿到手机号
        VipUser vipUser = super.selectOne(wrapper);
        // 验证手机号是否被注册
        AssertUtil.isNull(vipUser, "手机号已被注册");
        // 判断手机验证码
        // redis中拿到手机号
        String key = String.format(RedisConstants.KEY_SMS_CODE, mobile);
        SendVerifyCodeTo phoneNum = (SendVerifyCodeTo) redisTemplate.opsForValue().get(key);
        AssertUtil.isNotNull(phoneNum, "验证码已过期，请重新获取");
        // 和传入的手机验证码比较
        AssertUtil.isEqualsIgnoreCase(phoneNum.getCode(), smsCode, "验证码错误");

        // 保存Login 手机号就是用户名
        Login login = new Login();
        login.setUsername(mobile);
        // TODO
        login.setPassword(password);
        // 根据登录用户类型 前台人员
        login.setType(BaseConstants.Login.TYPE_USER);
        AjaxResult ajaxResult = loginFeignClient.save(login);
        System.out.println(ajaxResult);
        // 判断保存登录有没有成功
        AssertUtil.isTrue(ajaxResult.isSuccess(), "保存登录失败");
        AssertUtil.isNotNull(ajaxResult.getResultObj(), "保存登陆失败");
        // 登录ID
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());

        // 保存User
        VipUser user = new VipUser();
        user.setCreateTime(new Date().getTime());
        user.setPhone(mobile);
        user.setNickName(mobile);
        // 默认注册成功的标识
        long state = BitStatesUtils.addState(user.getBitState(), BitStatesUtils.OP_ACTIVED);
        // 绑定手机的标识
         state = BitStatesUtils.addState(state, BitStatesUtils.OP_AUTHED_PHONE);
         user.setBitState(state);
         user.setSecLevel(0);
         user.setLoginId(loginId);
         super.insert(user);

         // 保存Base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(user.getCreateTime());
        vipBase.setSsoId(user.getId());
        // 注册渠道
        vipBase.setRegChannel(regChannel);
        vipBase.setRegTime(vipBase.getCreateTime());
        vipBaseMapper.insert(vipBase);
        // 删除手机验证码
        redisTemplate.delete(key);
    }
}
